package com.ruoyi.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author tanjianjun
 * @version 1.o
 * @description BarcodeUtil
 * @since 2021/4/12 19:43
 */
public class BarcodeUtil {
    public static final String APPKEY = "66bdaaa9997c28ce";// 你的appkey
    public static final String URL = "https://api.jisuapi.com/barcode/read";
    public static final String barcode = "https://api.jisuapi.com/barcode/barcode/1471602033673149.png";

    public static String  Get(String pic) {
        String result = null;
        String barcode=null;
        String url = URL + "?appkey=" + APPKEY + "&barcode=" + pic;

        try {
            result = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONArray resultarr = json.optJSONArray("result");
                for (int i = 0; i < resultarr.size(); i++) {
                    JSONObject obj = (JSONObject) resultarr.opt(i);
                    String type = obj.getString("type");
                    String number = obj.getString("number");
                    System.out.println(type + " " + number);
                    if("CODE-128".equals(type)){
                        barcode=number;
                    }else{
                        continue;
                    }

                }
            }
            return barcode;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Get("http://60.205.249.142:8888/profile/upload/2021/04/14/6.jpeg");
    }
}
